﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Socket_Push_SDK
{
    public static class Socket_Push
    {
        #region 发送信息
        /// <summary>
        /// 直接发送信息
        /// </summary>
        /// <param name="Client"></param>
        /// <param name="Content"></param>
        public static void Send(this IClientProxy Client, string Content)
        {
            Client.SendAsync(Socket_Push_Builder.ReciveFuncName, Content).Wait();
        }
        /// <summary>
        /// 发送使用传入key
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="Content"></param>
        /// <returns>发送成功数</returns>
        public static int SendbyKey(string Key, string Content)
        {
            int Success = 0;
            var Clients = LinkPool.FindClientbyToken(Key);
            foreach (var Client in Clients)
            {
                Client.SendAsync(Socket_Push_Builder.ReciveFuncName, Content).Wait();
                Success++;
            }
            return Success;
        }
        /// <summary>
        /// 发送使用连接id
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static int SendbyConnectionID(string ID, string Content)
        {
            int Success = 0;
            var Clients = LinkPool.FindClientbyID(ID);
            foreach (var Client in Clients)
            {
                Client.SendAsync(Socket_Push_Builder.ReciveFuncName, Content).Wait();
                Success++;
            }
            return Success;
        }
        /// <summary>
        /// 向所有连接发送信息
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static int SendAll(string Content)
        {
            int Success = 0;
            var Clients = LinkPool.FindClients();
            foreach (var Client in Clients)
            {
                Client.SendAsync(Socket_Push_Builder.ReciveFuncName, Content).Wait();
                Success++;
            }
            return Success;
        }
        #endregion

        #region 关闭连接
        /// <summary>
        /// 使用连接id关闭连接
        /// </summary>
        /// <param name="ID"></param>
        public static void ClosebyConnectionID(string ID)
        {
            var Contexts = LinkPool.FindContextbyID(ID);
            foreach (var Context in Contexts)
            {
                Context.Abort();
            }
        }
        /// <summary>
        /// 使用传入key关闭连接
        /// </summary>
        /// <param name="Key"></param>
        public static void ClosebyKey(string Key)
        {
            var Contexts = LinkPool.FindContextbyToken(Key);
            foreach (var Context in Contexts)
            {
                Context.Abort();
            }
        }
        /// <summary>
        /// 关闭所有连接
        /// </summary>
        public static void CloseAll()
        {
            var Contexts = LinkPool.FindContexts();
            foreach (var Context in Contexts)
            {
                Context.Abort();
            }
        }
        #endregion

        #region 获取连接信息
        /// <summary>
        /// 获取所有连接信息
        /// </summary>
        /// <returns></returns>
        public static List<LinkInfo> GetLinkInfo()
        {
            return LinkPool.Find();
        }
        /// <summary>
        /// 使用连接id获取连接信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static List<LinkInfo> GetLinkInfobyConnectionID(string ID)
        {
            return LinkPool.FindbyID(ID).ToList();
        }
        /// <summary>
        /// 使用key获取连接信息
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static List<LinkInfo> GetLinkInfobyKey(string Key)
        {
            return LinkPool.FindbyToken(Key).ToList();
        }
        #endregion
    }
}
